<?
/**
* <b>pager</b>
*
* Pager class providing paging.
*
* @package gravy.classes
* @class pager extends gravy
* @author Lars Sorhus (lars@saucecode.com.au)
*/
class pager extends gravy{

	public $paging_variable="page";
	public $page=1;
	public $pages=0;
	public $recordCount=0;
	public $resultsPerPage=20;
	public $outputWindow=5;
	public $recordsFrom=0;
	public $recordsTo=0;
	public $output="";
	public static $fauxUrl=null;

	/**
         * <b>__construct</b>
         *
         * Calls base class(gravy) constructor
         * Sets some initial var's.
         *
         * @fn __construct
         * @param $recordCount - [def=0]
         * @param $resultsPerPage - [def=20]
         * @return void
         */
	public function __construct($recordCount=0,$resultsPerPage=20){
		parent::__construct();
		$this->recordCount=$recordCount;
		$this->resultsPerPage=$resultsPerPage;
		$this->page=(int)$_REQUEST[$this->paging_variable];
		if($this->page<1){
			$this->page=1;
		}
	}

	/**
         * <b>setRecordCount</b>
         *
         * Sets the record count.
         *
         * @fn setRecordCount
         * @param $rows - [def=0]
         * @return void
         */
	public function setRecordCount($rows=0){
		$this->recordCount=$rows;
		$this->pages=ceil($rows/$this->resultsPerPage);
		if($this->page>$this->pages){
			$this->page=$this->pages;
		}
		$this->output=$this->getOutput();
	}

	/**
         * <b>getPageUrl</b>
         *
         * Returns page url.
         *
         * @fn getPageUrl
         * @param $url - [def=NULL]
         * @param $ar - [def=NULL]
         * @return string
         */
	public function getPageUrl($url=NULL,$ar=NULL){
		$result="";
		if($url===NULL){
			$result=$this->getUrl(true,$ar,false,self::$fauxUrl);
		}else{
			$result=$url;
			if(is_array($ar)){
				$keys=array_keys($ar);
				for($c=0;$c<count($keys);$c++){
					$key=$keys[$c];
					$value=$ar[$key];
					$result=ereg_replace($key."=[^&]*","",$result);
					$result=ereg_replace("&{1,}","&",$result);
					if(substr_count($result,"?")<1){
						$result.="?";
					}
					if(substr($result,-1)=="?"||substr($result,-1)=="&"){
						$result.="$key=$value";
					}else{
						$result.="&$key=$value";
					}
				}
			}
		}
		return($result);
	}

	/**
         * <b>getOutput</b>
         *
         * Returns output string for paging function.
         *
         * @fn getOutput
         * @return string
         */
	public function getOutput(){
		$result="";
		#$this->page=13;
		if($this->pages>1){
			$result.='<div class="paging"><ul>';
			if($this->page>1){
				$result.='<li class="first"><a href="'.$this->getUrl(true,array("page"=>1),false,self::$fauxUrl).'">First Page</a></li>';
				$result.='<li class="previous"><a href="'.$this->getUrl(true,array("page"=>($this->page-1)),false,self::$fauxUrl).'">Previous Page</a></li>';
			}
			$from=$this->page-ceil($this->outputWindow/2);
			if($from+$this->outputWindow>$this->pages){
				$from=$this->pages-$this->outputWindow+1;
			}
			if($from<0){
				$from=1;
			}
			$to=$from+($this->outputWindow)-1;
			if($to>$this->pages){
				$to=$this->pages;
			}
			$this->recordsFrom=1+($this->page*$this->resultsPerPage - $this->resultsPerPage);
			$this->recordsTo=($this->page*$this->resultsPerPage);
			if($this->recordsTo>$this->recordCount){
				$this->recordsTo=$this->recordCount;
			}
			for($c=$from;$c<=$to;$c++){
				$selected="";
				if($c==$this->page){
					$selected=" selected";
				}
				$result.='<li class="page'.$selected.'"><a href="'.$this->getUrl(true,array("page"=>$c),false,self::$fauxUrl).'">'.$c.'</a></li>';
			}
			if($this->page<$this->pages){
				$result.='<li class="next"><a href="'.$this->getUrl(true,array("page"=>$this->page+1),false,self::$fauxUrl).'">Next Page</a></li>';
				$result.='<li class="last"><a href="'.$this->getUrl(true,array("page"=>$this->pages),false,self::$fauxUrl).'">Last Page</a></li>';
			}
			$result.='</ul></div>';
		}else{
			$this->recordsFrom=1;
			$this->recordsTo=$this->recordCount;
		}
		return($result);
	}

	/**
	 * <b>output</b>
	 *
	 * Wrapper function which prints and returns the result of
	 * this->getOutput()
	 *
	 * @fn output
	 * @param $url - [def=NULL]
	 * @return string
	 */
	public function output($url=NULL){
		$result=$this->getOutput($url);
		print($result);
		return($result);
	}

	/**
	 * <b></b>
	 * @return string
	 */
	static public function instance(application $gravy=NULL){
		$conf=ENV_MODE;
		$conf=new $conf;
		$rpp=$conf->resultsPerPage;
		$urpp=$_REQUEST[$conf->resultsPerPageVariable];
		if(strlen($urpp)>0){
			if((int)$urpp!=(int)$conf->resultsPerPage&&$conf->allowPagingOverride){
				$rpp=min($urpp,(int)$conf->maxResultsPerPage);
			}
		}
		$page=(int)$_REQUEST[$conf->pageVariable];
		$page=max($page,1);
		pager::outputWindow($conf->pagingOutputWindow);
		pager::resultsPerPage($rpp);
		pager::page($page);
		return(true);
	}

	static public function get($key="",$dataset="default"){
		$result=false;
		if($GLOBALS["pagers"][$dataset]){
			$data=$GLOBALS["pagers"][$dataset];
			if(is_array($data)){
				if(array_key_exists($key,$data)){
					$result=$data[$key];
				}else{

				}
			}else{

			}
		}else{

		}
		return($result);
	}

	static public function set($key="",$value="",$dataset="default"){
		$result=false;
		if(!is_array($GLOBALS["pagers"])){
			$GLOBALS["pagers"]=array();
		}
		if(!is_array($GLOBALS["pagers"][$dataset])){
			$GLOBALS["pagers"][$dataset]=array();
		}
		$GLOBALS["pagers"][$dataset][$key]=$value;
		return(true);
	}

	static public function links($dataset="default"){
		$result="";
		$gravy=new gravy();
		$page=self::page(NULL,$dataset);
		$pages=self::pages(NULL,$dataset);
		$outputWindow=self::outputWindow(NULL,$dataset);
		$recordsFrom=self::recordsFrom(NULL,$dataset);
		$resultsPerPage=self::resultsPerPage(NULL,$dataset);
		$recordCount=self::recordCount(NULL,$dataset);
		if($pages>1){
			$result.='<div class="paging"><ul>';
			if($page>1){
				$result.='<li class="first"><a href="'.$gravy->getUrl(true,array("page"=>1),false,self::$fauxUrl).'"><span>First</span></a></li>';
				$result.='<li class="previous"><a href="'.$gravy->getUrl(true,array("page"=>($page-1)),false,self::$fauxUrl).'"><span>Previous</span></a></li>';
			}
			$from=$page-ceil($outputWindow/2);
			if($from+$outputWindow>$pages){
				$from=$pages-$outputWindow+1;
			}
			if($from<1){
				$from=1;
			}
			$to=$from+($outputWindow)-1;
			if($to>$pages){
				$to=$pages;
			}
			for($c=$from;$c<=$to;$c++){
				$selected="";
				if($c==$page){
					$selected=" selected";
				}
				$result.='<li class="page'.$selected.'"><a href="'.$gravy->getUrl(true,array("page"=>$c),false,self::$fauxUrl).'"><span>'.$c.'</span></a></li>';
			}
			if($page<$pages){
				$result.='<li class="next"><a href="'.$gravy->getUrl(true,array("page"=>$page+1),false,self::$fauxUrl).'"><span>Next</span></a></li>';
				$result.='<li class="last"><a href="'.$gravy->getUrl(true,array("page"=>$pages),false,self::$fauxUrl).'"><span>Last</span></a></li>';
			}
			$result.='</ul></div>';
		}else{
			self::recordsFrom(1,$dataset);
			self::recordsTo($recordCount,$dataset);
		}
		return($result);
	}

	static public function resultsPerPage($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("resultsPerPage",$dataset));
		}else{
			return(self::set("resultsPerPage",$value,$dataset));
		}
	}

	static public function recordsFrom($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("recordsFrom",$dataset));
		}else{
			return(self::set("recordsFrom",$value,$dataset));
		}
	}

	static public function recordsTo($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("recordsTo",$dataset));
		}else{
			return(self::set("recordsTo",$value,$dataset));
		}
	}

	static public function pages($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("pages",$dataset));
		}else{
			return(self::set("pages",$value,$dataset));
		}
	}

	static public function page($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("page",$dataset));
		}else{
			return(self::set("page",$value,$dataset));
		}
	}

	static public function recordCount($recordCount=NULL,$dataset="default"){
		if($recordCount===NULL){
			return((int)self::get("recordCount",$dataset));
		}else{
			$page=self::page();
			$resultsPerPage=self::resultsPerPage();
			$pages=ceil($recordCount/self::resultsPerPage());
			self::pages($pages);
			if($page>$pages){
				self::page($pages);
			}
			$recordsTo=$page*$resultsPerPage;
			self::recordsFrom(1+($page*$resultsPerPage-$resultsPerPage),$dataset);
			if($recordsTo>$recordCount){
				$recordsTo=$recordCount;
			}
			self::recordsTo($recordsTo,$dataset);
			return(self::set("recordCount",$recordCount,$dataset));
		}
	}

	static public function outputWindow($value=NULL,$dataset="default"){
		if($value===NULL){
			return((int)self::get("outputWindow",$dataset));
		}else{
			return(self::set("outputWindow",$value,$dataset));
		}
	}
};
?>
